package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.PlatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.PlatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.PlatMchntInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.PlatMchntWechatInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.PlatMchntWechatInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;

public class PlatMchntWechatInfoQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        PlatMchntWechatInfoQueryRequest platMchntWechatInfoQueryRequest = new PlatMchntWechatInfoQueryRequest();
        ConvertUtil.convertOutput(platMchntWechatInfoQueryRequest);
        return platMchntWechatInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        PlatMchntWechatInfoQueryRequest platMchntWechatInfoQueryRequest = (PlatMchntWechatInfoQueryRequest) request;
        PlatMchntWechatInfoQueryResponse platMchntWechatInfoQueryResponse = new PlatMchntWechatInfoQueryResponse();
        BizResponse<PlatMchntWechatInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);

        String instId = platMchntWechatInfoQueryRequest.getHeader().getInstId();
        String platMchntNo = platMchntWechatInfoQueryRequest.getPlatMchntNo();
        PlatMchntInfoMapper platMchntInfoMapper = new PlatMchntInfoMapperImpl();
        PlatMchntInfo platMchntInfo = platMchntInfoMapper.selectPlatMchntInfoByMchntNo(instId, platMchntNo);
        if (null == platMchntInfo) {
            rglog.error("平台商户号<{}>无平台商户信息!", platMchntNo);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(platMchntWechatInfoQueryResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(instId, platMchntNo);

        if (null == wechatMchntInfo) {
            rglog.error("平台商户号<{}>无微信商户信息!", platMchntNo);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(platMchntWechatInfoQueryResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }


        platMchntWechatInfoQueryResponse.setPlatMchntNo(platMchntNo);
        platMchntWechatInfoQueryResponse.setPlatMchntName(platMchntInfo.getPlatMchntName());
        platMchntWechatInfoQueryResponse.setWechatAppid(wechatMchntInfo.getAppId());
        platMchntWechatInfoQueryResponse.setWechatAppidKey(wechatMchntInfo.getAppSecretKey());
        platMchntWechatInfoQueryResponse.setWechatMiniAppid(wechatMchntInfo.getMiniAppId());
        platMchntWechatInfoQueryResponse.setWechatMiniAppidKey(wechatMchntInfo.getMiniSecretKey());

        platMchntWechatInfoQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        platMchntWechatInfoQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(platMchntWechatInfoQueryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PlatMchntWechatInfoQueryResponse platMchntWechatInfoQueryResponse = (PlatMchntWechatInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(platMchntWechatInfoQueryResponse);
    }
}
